package com.itzhigui.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 支付宝配置类
 *
 * @author java1412
 * @since 2023-11-16 09:51:03
 */
@Component
public class AlipayConfig implements ApplicationRunner {
    @Value("${alipay.appId}")
    private String appId;

    @Value("${alipay.privateKey}")
    private String privateKey;

    @Value("${alipay.publicKey}")
    private String publicKey;

    @Value("${alipay.gateway}")
    private String gateway;

    @Value("${alipay.returnUrl}")
    private String returnUrl;

    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    // 项目初始化
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化支付宝SDK
        Factory.setOptions(getOptions());
        System.out.println("*********************支付宝SDK初始化成功***************************");
    }

    private Config getOptions() {
        Config config = new Config();
        config.protocol = "https"; // 设置协议
        config.gatewayHost = this.gateway; // 设置网关
        config.signType = "RSA2"; // 设置签名算法
        config.appId = this.appId; // 设置应用id
        config.merchantPrivateKey = this.privateKey;    // 设置私钥
        config.alipayPublicKey = this.publicKey;    // 设置支付公钥
        config.notifyUrl = this.notifyUrl;  // 设置支付成功的回调地址(接口)，走公网
        return config;
    }

    public String getReturnUrl() {
        return returnUrl;
    }
}
